/**
 * 
 */
package cx3k.dataaccess;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cx3k.dataaccess.search.Searcher;
import cx3k.dataaccess.search.Searcher.SearchField;
import cx3k.scheduler.objects.Course;
import cx3k.scheduler.objects.GroupedCourse;
import cx3k.scheduler.objects.Major;
import cx3k.scheduler.objects.Minor;
import cx3k.scheduler.objects.Semester;

/**
 * @author Chris
 *
 */
public abstract class CX3KDatabaseManager<T> extends DatabaseManager<T> {

	public static Map<Integer, GroupedCourse> getGroupedCourse(String degreeProgram, String courseNum, String major){
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		
//		session.createCriteria("from GroupedCourse as groupedCourse where groupedCourse.");
		
		tx.commit();
		session.close();
		
		return null;
	}
		
	public static List<?> executeHQL(String HQL){
		
		Session session = getSession();
		Transaction tx = session.beginTransaction();
		
		List<?> results = session.createQuery(HQL).list();
		
		tx.commit();
		session.close();
		
		return results;
	}
	
	protected static Map<Integer, Major> getAllMajors(){
		return createMajorMapping(getMajorsFromDatabase());
	}
	
	protected static Map<Integer, Minor> getAllMinors(){
		return createMinorMapping(getMinorsFromDatabase());
	}
	
	protected static Map<Integer, Course> getAllCourses(){
		return createCourseMapping(getCoursesFromDatabase());
	}
	
	protected static Map<Integer, Semester> getAllSemesters(){
		return createSemesterMapping(getSemestersFromDatabase());
	}
	
	protected static Course getCourse(int courseId){
		return getCourseFromDatabase(courseId);
	}
	
	public static void main(String[] args){
		getCourseFromDatabase(3011453);
	}
	
	@SuppressWarnings("unchecked")
	private static Course getCourseFromDatabase(int courseId){
		
		Course ret = null;
		Searcher searcher = new Searcher(Course.class);
		searcher.searchForExactValue(SearchField.COURSE_ID, ((Integer) courseId).toString());
		
		List<Course> courses = (List<Course>) searcher.execute();
		
		if(!courses.isEmpty()){
			ret = courses.get(0);
		}
		
		return ret;
	}
	
	@SuppressWarnings("unchecked")
	private static List<Major> getMajorsFromDatabase(){
		return (List<Major>) new Searcher(Major.class).execute();
	}
	
	@SuppressWarnings("unchecked")
	private static List<Minor> getMinorsFromDatabase(){
		return (List<Minor>) new Searcher(Minor.class).execute();
	}
	
	@SuppressWarnings("unchecked")
	private static List<Course> getCoursesFromDatabase(){
		return (List<Course>) new Searcher(Course.class).execute();
	}
	
	@SuppressWarnings("unchecked")
	private static List<Semester> getSemestersFromDatabase(){
		return (List<Semester>) new Searcher(Semester.class).execute();
	}
	
	private static Map<Integer, Major> createMajorMapping(List<Major> majors){
		Map<Integer, Major> mappedMajors = new HashMap<Integer, Major>();
		
		for(Major major : majors){
			mappedMajors.put(major.getMajorId(), major);
		}
		
		return mappedMajors;
	}
	
	private static Map<Integer, Minor> createMinorMapping(List<Minor> minors){
		Map<Integer, Minor> mappedMinors = new HashMap<Integer, Minor>();
		
		for(Minor minor : minors){
			mappedMinors.put(minor.getMinorId(), minor);
		}
		
		return mappedMinors;
	}
	
	private static Map<Integer, Course> createCourseMapping(List<Course> courses){
		Map<Integer, Course> mappedCourses = new HashMap<Integer, Course>();
		
		for(Course course : courses){
			mappedCourses.put(course.getCourseId(), course);
		}
		
		return mappedCourses;
	}
	
	private static Map<Integer, Semester> createSemesterMapping(List<Semester> semesters){
		Map<Integer, Semester> mappedSemesters = new HashMap<Integer, Semester>();
		
		for(Semester semester : semesters){
			mappedSemesters.put(semester.getSemesterId(), semester);
		}
		
		return mappedSemesters;
	}
}
